package com.esyoga.yoyoga.handler;

import com.esyoga.yoyoga.enums.UserTypeEn;
import com.esyoga.yoyoga.pojo.TPlatform;
import com.esyoga.yoyoga.service.PlatformVerifyService;
import com.esyoga.yoyoga.util.EncryptionUtil;
import com.esyoga.yoyoga.util.UsernamePasswordByUserTypeToken;
import lombok.Data;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Data
@Controller
@RequestMapping("/platformVerify")
public class PlatformVerifyHandler {
    @Resource
    private PlatformVerifyService platformVerifyService;

    @RequestMapping("/platformLoginByAccount")
    @ResponseBody
    public Map<String,Object> platformLoginByAccount(String account, String pPassword, int UserType, HttpServletRequest request){
        if(account==null || pPassword==null){
            return null;
        }
        //获取subject对象
        Subject currentUser = SecurityUtils.getSubject();
        Map<String,Object> map = new HashMap<>();
        map.put("msg","您已登录，切勿重复登录");
        //如果验证过了就直接跳过
        if (!currentUser.isAuthenticated()) {
            HttpSession httpSession = request.getSession();
            Session currentUserSession = currentUser.getSession();

            UsernamePasswordByUserTypeToken token = new UsernamePasswordByUserTypeToken(account, pPassword, UserType);

            try{
                //将用户类型、姓名存储到session中
                currentUserSession.setAttribute("account", account);
                currentUserSession.setAttribute("userType", UserType);
                currentUser.login(token);
                TPlatform tPlatform = platformVerifyService.findPlatformByAccount(account);
                //存入用户类型和pojo到session
                httpSession.setAttribute("userType",UserType);
                httpSession.setAttribute("tPlatform",tPlatform);
                httpSession.setAttribute("userTypeEn", UserTypeEn.PLATFORM);

                System.out.println("登录成功");
                map.put("msg", "登陆成功");
                return map;
            }catch (UnknownAccountException e) {
                System.out.println("用户不存在");
                map.put("success", false);
                map.put("msg", "用户不存在");
            } catch (IncorrectCredentialsException e) {
                System.out.println("密码不对");
                map.put("success", false);
                map.put("msg", "密码不正确");
            } catch (LockedAccountException e) {
                System.out.println("账户被锁定");
                map.put("success", false);
                map.put("msg", "账号被锁定");

            } catch (Exception e) {
                System.out.println("其他异常");
                map.put("success", false);
                map.put("msg", "其他异常");
            }

        }

        return map;
    }


}
